package com.yjhb.android.sbz.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatRadioButton;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.Timer;
import com.yjhb.android.sbz.manager.ResManager;
import com.yjhb.android.sbz.mvp.settingtimer.SettingTimerPresenter;
import com.yjhb.android.sbz.mvp.settingtimer.SettingTimerView;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.tool.ZxTimeTool;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.tool.ZxTool;

import org.greenrobot.eventbus.EventBus;

import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2021/1/12 16:15
 */
public class SettingTimerActivity extends BaseActivity<SettingTimerPresenter> implements SettingTimerView {
    private final String TAG = "SettingTimerActivity";
    @BindView(R.id.et_price)
    EditText etPrice;
    @BindView(R.id.topTime_tv)
    TextView topTimeTv;
    @BindView(R.id.ll_topTime)
    LinearLayout llTopTime;
    @BindView(R.id.recommend_ll)
    LinearLayout recommend_ll;
    @BindView(R.id.et_time_length)
    EditText etTimeLength;
    @BindView(R.id.radio_1)
    AppCompatRadioButton radio1;
    @BindView(R.id.radio_2)
    AppCompatRadioButton radio2;
    @BindView(R.id.rg_repeat)
    RadioGroup rgRepeat;
    @BindView(R.id.addTimer_tv)
    TextView addTimerTv;
    @BindView(R.id.title_tv)
    TextView title_tv;


    Date time;
    Integer taskId;
    int timerType = 1; //1定时置顶 2定时刷新

    @Override
    protected SettingTimerPresenter initPresenter() {
        return new SettingTimerPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting_timer;
    }

    @Override
    protected void initUI() {
        taskId = getIntent().getIntExtra("taskId", -1);
        if (taskId == -1) {
            ZxToast.showINFOBottomToast("任务ID异常，请联系管理员。");
            finish();
        }
        timerType = getIntent().getIntExtra("timerType", -1);
        if (timerType == -1) {
            ZxToast.showINFOBottomToast("计时器类型异常，请联系管理员。");
            finish();
        }
        if (timerType == 1) {
            title_tv.setText("定时置顶");
            topTimeTv.setText("请选择开始置顶的时间");
            recommend_ll.setVisibility(View.VISIBLE);
        } else if (timerType == 2) {
            title_tv.setText("自动刷新");
            topTimeTv.setText("请选择开始刷新的时间");
            recommend_ll.setVisibility(View.GONE);
        }

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void regEvent() {

    }

    @OnClick(R.id.back)
    void back() {
        finish();
    }

    @OnClick({R.id.topTime_tv, R.id.topTime_more_tv})
    void topTime() {
        //时间选择器
        ZxTool.closeKeybord(etTimeLength, this);
        TimePickerView pvTime = new TimePickerBuilder(this, (date, v) -> {
            this.time = date;
            topTimeTv.setText(ZxTimeTool.getDate(time.getTime(), "yyyy年MM月dd日 HH:mm"));
        })
                .setType(new boolean[]{true, true, true, true, true, false})// 默认全部显示
                .setCancelColor(ResManager.getThemeColor())
                .setSubmitColor(ResManager.getThemeColor())
                .setRangDate(Calendar.getInstance(), null)
                .isCyclic(true)
                .build();
        if (time != null) {
            Calendar time = Calendar.getInstance();
            time.setTime(this.time);
            pvTime.setDate(time);
        }
        pvTime.show();
    }

    @OnClick(R.id.addTimer_tv)
    void addTimer() {
        Timer timer = new Timer();
        if (time == null) {
            ZxToast.showINFOBottomToast("请选择置顶开始的时间哦~~");
            return;
        }
        timer.setStartDay(ZxTimeTool.getDate(time.getTime(), "yyyy-MM-dd 00:00:00"));
        timer.setStartTime(ZxTimeTool.getDate(time.getTime(), "yyyy-MM-dd HH:mm:00"));
        if (timerType == 1) {
            String hour = Tool.getText(etTimeLength);
            if (TextUtils.isEmpty(hour)) {
                ZxToast.showINFOBottomToast("置顶持续时长没有填写哦~~");
                return;
            }
            timer.setHours(Integer.parseInt(hour));
        } else {
            timer.setHours(1);
        }
        timer.setIsLoop(rgRepeat.getCheckedRadioButtonId() == R.id.radio_1 ? 1 : 0);
        timer.setTaskId(taskId);
        timer.setUserId(Integer.parseInt(App.userInfo.getId()));
        timer.setType(timerType);
        presenter.addTimer(timer);
    }

    @Override
    public void onWait(String tips) {
        loadingDialog.setTipsText(tips);
        loadingDialog.show();
    }

    @Override
    public void onSuccess(int type,Object o) {
        loadingDialog.cancel();
        EventBus.getDefault().post(new EventMsg(EventMsg.REQUEST_REFRESH_TIMER_LIST,null));
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        ZxToast.showINFOBottomToast(msg);
    }
}
